<template>
	<view class="page">
		<view class="flex row-between box">
			<view>会员费用</view>
			<view>￥{{money}}</view>
		</view>
		<view @click="submit" class="m-t-100 bold" v-if="isvip==0">
			<u-button shape="circle" size="large" type="primary" text="去支付"></u-button>
		</view>
	</view>
</template>

<script>
	const app = getApp();
	export default {
		data() {
			return {
				isvip:0,
				money:app.globalData.vip_price
			}
		},
		onLoad(options) {
			this.isvip = options.isvip;
			if(options.isvip==1){
				this.money = options.paymoney;
			}
		},
		methods: {
			async submit(){
				let res = await this.$api.vip_pay();
				if (res.code == 1) {
					// #ifdef APP-PLUS
					await this.paymoney(res.data.order_id,res.data.from,6)
					// #endif
					// #ifdef MP-WEIXIN
					await this.paymoney(res.data.order_id,res.data.from,1)
					// #endif
				}
			},
			async paymoney(id,str,type){
				let that = this;
				uni.showLoading()
				let res = await this.$api.prepay({
					order_id:id,
					from:str,
					type:type,
					pay_way:2
				});
				if (res.code == 1) {
					uni.requestPayment({
						provider: 'wxpay',
						timeStamp: res.data.config.timeStamp, // 时间戳（单位：秒）
						nonceStr: res.data.config.nonceStr, // 随机字符串
						package: res.data.config.package, // 固定值
						signType: res.data.config.signType, // 签名算法，这里用的 MD5/RSA 签名
						paySign: res.data.config.paySign, //签名
						success(res) {
							that.$toast({
								title: '支付成功！',
								icon: 1
							});
							app.globalData.is_vip = 1;
							setTimeout(function () {
								uni.navigateBack();
							}, 1000);
						},
						fail(e) {
							that.$toast({
								title: '支付取消！'
							});
						}
					});
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
.page{
	padding: 80rpx 35rpx;
	font-size: 30rpx;
	
	.box{
		border: 1px solid #eee;
		border-radius: 25rpx;
		padding: 30rpx 25rpx;
		
		view:last-child{
			color: #457FD3;
		}
	}
}
</style>
